package com.imtimmy.android.pda;

import java.io.File;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ViewActivity extends Activity {
	private TextView tv_Title;
	private TextView tv_Date;
	private TextView tv_Content;
	private TextView tv_Reminders;
	private ImageView iv_Image;
	private ImageButton ib_Play;
	private LinearLayout background;

	private String sImagePath = "";
	private String sSoundPath = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.view_activity);

		background = (LinearLayout) findViewById(R.id.activity_background);
		tv_Title = (TextView) findViewById(R.id.tv_Title_viewactivity);
		tv_Date = (TextView) findViewById(R.id.tv_Date_viewactivity);
		tv_Content = (TextView) findViewById(R.id.tv_Content_viewactivity);
		tv_Reminders = (TextView) findViewById(R.id.tv_Reminders_viewactivity);
		iv_Image = (ImageView) findViewById(R.id.imageView_image);
		ib_Play = (ImageButton) findViewById(R.id.imageButton_play);

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		int nId = bundle.getInt(DatabaseHelper.COLUMN_ID);
		Long lDate = bundle.getLong(DatabaseHelper.COLUMN_DATE);
		String sDate = new Date(lDate).toLocaleString();
		String sTitle = bundle.getString(DatabaseHelper.COLUMN_TITLE);
		String sContent = bundle.getString(DatabaseHelper.COLUMN_CONTENT);
		sImagePath = bundle.getString(DatabaseHelper.COLUMN_IMAGE);
		sSoundPath = bundle.getString(DatabaseHelper.COLUMN_SOUND);
		Drawable image = getResources().getDrawable(R.drawable.no_pic);
		if (sImagePath.length() > 0) {
			image = Drawable.createFromPath(sImagePath);
		}
		int color = bundle.getInt(DatabaseHelper.COLUMN_COLOR);

		String test = "Id: " + nId + "\n";
		test += "Date: " + lDate + "\n";
		test += "Title: " + sTitle + "\n";
		test += "Content : " + sContent + "\n";
		test += "Image: " + sImagePath + "\n";
		test += "Sound: " + sSoundPath + "\n";

		background.setBackgroundColor(color);
		tv_Title.setText(sTitle);
		tv_Title.setTextColor(color);
		tv_Date.setText(sDate);
		tv_Date.setTextColor(Color.BLUE);

		tv_Content.setText(sContent);
		iv_Image.setImageDrawable(image);
		iv_Image.setOnClickListener(imageView_ImageOnClickListener);

		ib_Play.setOnClickListener(ib_PlayOnClickListener);
		if (sSoundPath.length() > 0) {
			ib_Play.setVisibility(View.VISIBLE);
		} else {
			ib_Play.setVisibility(View.GONE);
		}

		tv_Reminders.setText("testtest");

		// textView.setText(test);
	}

	private OnClickListener imageView_ImageOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (sImagePath.length() > 0) {
				File file = new File(sImagePath);
				Intent intent = new Intent(Intent.ACTION_VIEW);
				Uri uri = Uri.fromFile(file);
				intent.setDataAndType(uri, "image/*");
				startActivity(intent);
			}
		}
	};

	private OnClickListener ib_PlayOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (sSoundPath.length() > 0) {
				File file = new File(sSoundPath);
				Intent intent = new Intent();
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.setAction(Intent.ACTION_VIEW);
				// intent.setDataAndType(Uri.fromFile(file), getMIMEType(file));
				intent.setDataAndType(Uri.fromFile(file), "audio/*");
				startActivity(intent);
			}
		}
	};

}
